<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
class Sharecates extends Common
{

    //共享网址分类
    public function index()
    {   
        if(request() -> isPost()){
            $key = input('post.key');
            $page = input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):10;
            $list = db('common_block_cate')
                 -> where('id|name','like','%'.$key.'%')
                 ->order('id desc')
                 -> paginate(array('list_rows'=>$pageSize,'page'=>$page))
                 ->toArray();   
            return json(['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1]);
        }
        return $this->fetch();
    }

    //添加共享网址分类
    public function add()
    {   
        if(request() ->isPost()){
           $data['name'] = input('post.name');
           $name = db('common_block_cate')->where($data)->find();
           if($name){
                return json(['code'=>0,'msg'=>'添加失败,分类名已经存在']);
           }else{
                $data['addtime'] = date('Y-m-d H:i:s');
                $cate = db('common_block_cate')->insertGetId($data);
                if($cate){
                    return json(['code'=>1,'msg'=>'添加成功','url' => url('sharecates/index')]);
                }else{
                    return json(['code'=>0,'msg'=>'添加失败']);
                }
           }
        }else{    
           return $this->fetch();
        }
    }

    //编辑共享网址分类
    public function edit($id='')
    {
        if(request() -> isPost()){
            $id = input('post.id');
            $name = trim(input('post.name'));
            $names = db('common_block_cate')->where('name',$name)->where('id','<>',$id)->find();
            if($names){
                return json(['code'=>0,'msg'=>'编辑失败,分类名已经存在']);
            }else{
                $update =db('common_block_cate')->where('id', $id)->update(['name' => $name]);
                if($update){
                    return json(['code'=>1,'msg'=>'编辑成功','url' => url('sharecates/index')]);
                }else{
                    return json(['code'=>0,'msg'=>'编辑失败']);
                }
            }
        }else{
            $list = db('common_block_cate')->field('id,name')->where('id',$id)->find();
            $this->assign('list',$list);
            return $this->fetch();
        }
    }

    //删除单条记录(删除数据)
    public function del()
    {   
        $data['id'] = input('param.id');  
        if($data){
            $del = db('common_block_cate')->where($data)->delete();
            if($del){
                return json(['code'=>1,'msg'=>'删除成功']);
            }else{
                return json(['code'=>0,'msg'=>'删除失败']);
            }
        }
        
    }

    //删除单条记录(删除数据)
    public function delall()
    {   
        if(request() -> isPost()){
            $ids = $_POST['ids'];  
            $all = db('common_block_cate')->delete($ids);
            if($all){
                return json(['code'=>1,'msg'=>'删除成功']);
            }else{
                return json(['code'=>0,'msg'=>'删除失败']);
            }
        }
    }
}